<?php
/**
 * regd by PhpStorm.
 * User: lee
 * Date: 2017/11/29
 * Time: 17:31
 */

namespace Biz\validate;

use Biz\service\Category as sCategory;
use Biz\model\brand\Brand as mBrand;
use Biz\module\Packages\WordAnalyze\StyleWord as beStyleWord;
use Biz\module\Packages\WordAnalyze\ItemWord as beItemWord;
use Biz\model\GoodsExtendInfo as mGoodsExtendInfo;
use Biz\model\GoodsBasicInfo as mGoodsBasicInfo;


class GoodsBasicInfo extends \Doraemon\pockets\validate\Base
{


    public static function rules()
    {

        return [
            ['uid', 'required', null, '请正确传入用户UID', ['create', 'update']],

            ['title', 'required', null, '请填写商品名称', ['create', 'update']],
            ['title', 'length', '1,50', '商品名称最大50个字符', ['create', 'update']],
            ['title', 'regex', ['pattern' => '/^[\x{4e00}-\x{9fa5}_\-a-zA-Z0-9\s]+$/u'], '商品名称不能包含特殊字符，请修改商品名', ['create', 'update']],
//            ['title', 'callback', ['callback' => [new self(), 'checkTitleStyleWord']], '商品名称最多只能包含1个风格词，请修改商品名', ['create', 'update']],
//            ['title', 'callback', ['callback' => [new self(), 'checkTitleItemWord']], '商品名称最多只能包含2个物品词，请修改商品名', ['create', 'update']],


            ['second_category_id', 'required', null, '请选择分类', ['create']],
            ['second_category_id', 'callback', ['callback' => [new self(), 'checkSecondCategoryId']], '请正确选择商品分类，或您没有该分类经营权', ['create']],
            ['second_category_id', 'callback', ['callback' => [new self(), 'checkEditCategoryAndBrand']], '商品分类不可修改', ['update']],


            ['brand_id', 'callback', ['callback' => [new self(), 'checkBrandId']], '请正确填写品牌信息', ['create', 'update']],
            ['brand_id', 'callback', ['callback' => [new self(), 'checkEditCategoryAndBrand']], '品牌不可修改', ['update']],

            ['spec_img', 'required', null, '请上传商品主图', ['create', 'update']],

        ];

    }

    public function checkTitleStyleWord($value, $valueIdentifier, $allData)
    {

        //风格词只要出现1个以上就报错
        if ((new beStyleWord())->getWordTimes($value) > 1) {
            return false;
        }

        return true;
    }

    public function checkTitleItemWord($value, $valueIdentifier, $allData)
    {
        //物品词只要出现2个以上就报错
        if ((new beItemWord())->getWordTimes($value) > 2) {
            return false;
        }

        return true;
    }

    public function checkSecondCategoryId($value, $valueIdentifier, $allData)
    {

        $categories = sCategory::mgtGetSupportTreeInfo($allData['uid'], $value);

        return (isset($categories['list'][2]['id']) && $categories['list'][2]['id'] > 0) ? true : false;
    }

    public function checkEditCategoryAndBrand($value, $valueIdentifier, $allData)
    {

        $goodsInfo = mGoodsBasicInfo::getInfo($allData['goods_id'], ['brand_id', 'uid', 'had_online','second_category_id']);

        //检查是否基于wiki创建的
        if (mGoodsExtendInfo::checkGoodsByWiki($allData['goods_id'])) {
            if ((isset($allData['second_category_id']) && $goodsInfo['second_category_id'] > 0) && $allData['second_category_id'] != $goodsInfo['second_category_id']) {
                return false;
            }

//            if (isset($allData['brand_id']) && $goodsInfo['brand_id']>0 && $allData['brand_id'] != $goodsInfo['brand_id']) {
//                return false;
//            }
        } else {
            //商家直接创建的商品，商品分类不可修改
            if ((isset($allData['second_category_id']) && $goodsInfo['second_category_id']>0 && $allData['second_category_id'] != $goodsInfo['second_category_id'])) {
                return false;
            }
        }


        //曾经上过架的商品的判断
        if ($goodsInfo['had_online'] == mGoodsBasicInfo::HAD_ONLINE_YES) {
            if ((isset($allData['second_category_id']) && $goodsInfo['second_category_id']>0 && $allData['second_category_id'] != $goodsInfo['second_category_id'])) {
                return false;
            }

            //如果已填写品牌，不可修改品牌
//            if (isset($allData['brand_id']) && $goodsInfo['brand_id']>0 && $goodsInfo['brand_id'] != $allData['brand_id']) {
//                return false;
//            }
        }

        return true;
    }


    public function checkBrandId($value, $valueIdentifier, $allData)
    {
        if(empty($value)){
            return true;
        }

        $info = mBrand::getInfo($value);

        if (empty($info)) {
            return false;
        }

        return true;
    }
}